<template>
  <div v-show="$store.state.chat.visible" class="chat-panel">
    <div class="contact-list">
      <a-row
        type="flex"
        align="middle"
        justify="space-between"
        v-for="(contact, key) in contactList"
        :key="key"
      >
        <div class="contact" @click="clickContact(contact.id)">
          <a-icon type="user" />
          {{ contact.name }}
        </div>
      </a-row>
    </div>
    <div class="panel">
      <div class="title">
        <div class="name">
          {{ getContactName() }}
        </div>
        <a-icon type="close" class="close-btn" @click="closePanel" />
      </div>
      <div class="chat-record">
        <div v-for="(v, k) in msgList" :key="k">
          <MsgBubble :bubble="v.msg" />
        </div>
      </div>
      <a-row type="flex" class="input" :gutter="5" justify="center">
        <a-col flex="auto">
          <a-textarea
            placeholder=""
            @change="onChange"
            :auto-size="{ minRows: 1, maxRows: 5 }"
          />
        </a-col>
        <a-col flex="64px"><a-button @click="send">发送</a-button></a-col>
      </a-row>
    </div>
  </div>
</template>
<script>
import { find } from "lodash";
import MsgBubble from "./msg-bubble.vue";
export default {
  name: "ChatPanel",
  components: {
    MsgBubble
  },
  props: {
    self: {}
  },
  data() {
    return {
      contactList: [],
      contactId: "",
      msg: "",
      msgList: []
    };
  },
  watch: {
    "global.sock": {
      handler(nval) {
        if (nval) {
          global.sock.onmessage = e => {
            this.msgList.push(JSON.parse(e.data));
          };
        }
      },
      deep: true
    }
  },
  methods: {
    onChange(e) {
      this.msg = e.target.value;
    },
    getContactName() {
      const { contactList, contactId } = this;
      return find(contactList, ["id", contactId])?.name ?? "";
    },
    closePanel() {
      this.$store.commit("chat/setVisible", { val: false });
    },
    clickContact(id) {
      this.$store.commit("chat/setContactId", { val: id });
    },
    send() {
      const { sock } = global;
      console.log(sock);
      if (sock) sock.send(this.msg);
      this.msg = "";
    }
  },
  watch: {
    "$store.state.chat.contactList": {
      handler(nval) {
        this.contactList = nval;
      },
      deep: true
    },
    "$store.state.chat.contactId": {
      handler(nval) {
        this.contactId = nval;
      },
      deep: true
    }
  }
};
</script>
<style lang="less" scoped>
@h: 500px;
@border: 1px solid #cccccc;
.chat-panel {
  .contact-list {
    width: 200px;
    height: @h;
    position: fixed;
    right: 400px;
    bottom: 0;
    background: white;
    z-index: 9;
    border: @border;
    .contact {
      width: 100%;
      cursor: pointer;
      padding: 10px;
      border-bottom: 1px solid #cccccc;
    }
  }
  .panel {
    width: 400px;
    height: @h;
    border-top: @border;
    border-bottom: @border;
    position: fixed;
    right: 0;
    bottom: 0;
    background: white;
    display: flex;
    flex-direction: column;
    justify-content: space-between;
    z-index: 9;
    .title {
      width: 100%;
      height: 25px;
      display: flex;
      padding: 5px;
      align-items: center;
      border-bottom: @border;
      .name {
        width: 100%;
        text-align: center;
      }
      .close-btn {
        position: absolute;
        right: 5px;
      }
    }
    .chat-record {
      width: 100%;
      height: 100%;
      display: flex;
      background: #f0f2f5;
      padding: 5px;
    }
    .input {
      width: 100%;
      padding: 5px;
      max-height: 145px;
      border-top: @border;
      margin: 0px !important;
    }
  }
}
</style>
